"""
串类
"""
import requests, time, os
from lxml import etree
from end import End
from libs import getDomain, joinStrip

class Thread(object):
    def __init__(self, url, background):
        response = requests.get(url)
        html = etree.HTML(response.text)
        self.main = html.xpath('//*[@id="h-content"]//div[@class="h-threads-item-main"]')
        self.replys = html.xpath('//*[@id="h-content"]//div[@class="h-threads-item-reply-main"]')
        self.background = background
        self.url = response.url
        self.downPageUrl = html.xpath('//*[@id="h-content"]//a[text()="下一页"]/@href')
        self.upPageUrl = html.xpath('//*[@id="h-content"]//a[text()="上一页"]/@href')

    def getAction(self, action):
        domain = getDomain(self.url)
        if action == 'up' or action == 'u':
            return Thread(domain + self.upPageUrl[0], self) if len(self.upPageUrl)>0 else self
        elif action == 'down' or action == 'd':
            return Thread(domain + self.downPageUrl[0], self) if len(self.downPageUrl)>0 else self
        elif action == 'menu':
            from menu import Menu
            return Menu()
        elif action == 'back' or action == 'b':
            return self.background
        # elif action.isdigit() and int(action) < len(self.threads):
        #     threadUrl = domain + self.threads[int(action)].xpath('//a[@class="h-threads-info-id"]/@href')[0]
        #     return Thread(threadUrl, self)
        elif action == 'f':
            return Thread(self.url, self.background)
        else:
            # TODO: 修改成使用WrongAction类
            print("无法识别")
            return self

    def show(self):
        if len(self.main) == 0:
            print("无法解析！请按f重试或按q退出。")
            return True
        main = self.main[0]
        title = joinStrip(main.xpath('.//span[@class="h-threads-info-title"]/text()'))
        email = joinStrip(main.xpath('.//span[@class="h-threads-info-email"]/text()'))
        createDate = joinStrip(main.xpath('.//span[@class="h-threads-info-createdat"]/text()'))
        uid = joinStrip(main.xpath('.//span[@class="h-threads-info-uid"]/text()'))
        url = joinStrip(main.xpath('.//a[@class="h-threads-info-id"]/text()'))
        content = joinStrip(main.xpath('.//div[@class="h-threads-content"]//text()'))
        print(title, email, createDate, uid, url)
        print('---------------------------------------')
        print(content)
        print('=======================================')

        for reply in self.replys:
            title = joinStrip(reply.xpath('.//span[@class="h-threads-info-title"]/text()'))
            email = joinStrip(reply.xpath('.//span[@class="h-threads-info-email"]/text()'))
            createDate = joinStrip(reply.xpath('.//span[@class="h-threads-info-createdat"]/text()'))
            uid = joinStrip(reply.xpath('.//span[@class="h-threads-info-uid"]/text()'))
            url = joinStrip(reply.xpath('.//a[@class="h-threads-info-id"]/text()'))
            content = joinStrip(reply.xpath('.//div[@class="h-threads-content"]//text()'))

            print(title, email, createDate, uid, url)
            print('---------------------------------------')
            print(content)
            print('=======================================')
        
        print('Up上一页/Down下一页/Back返回')
        return True

if __name__ == "__main__":
    t = Thread('https://adnmb2.com/t/6129442', 'None')
    t.show()